//
//  HomeViewController.m
//  FPTest
//
//  Created by zhaohong on 2017/11/9.
//  Copyright © 2017年 zhaohong. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTable.h"
#import "HomeModel.h"
@interface HomeViewController ()
@property (weak, nonatomic) IBOutlet HomeTable *tableView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"首页";
    
    //1.实现requestDataDelegate网络请求的几个个基本协议requestData，requestDataAgain
    //2.实现UITableViewEventDelegate的协议,将tableView传递给父类
    //3.在下拉刷新里面调用网络请求requestData
    
    [self requestData];
    
}

#pragma mark -------------------------requestDataDelegate-------------------------


#pragma mark 请求网路
- (void)requestData
{
    [super requestData];
    
    NSMutableArray *muArray = [NSMutableArray array];
    //做一个假的数据数组
    for(int i = 0; i<20; i++){
        HomeModel *model = [[HomeModel alloc]init];
        model.name = [NSString stringWithFormat:@"王%d",i];
        model.school = [NSString stringWithFormat:@"第%d中学",i];
        [muArray addObject:model];
    }
    
    
    //延迟3s模拟网络请求过程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      
        //将请求获取到的值赋予tableView并刷新,并设置此类为代理
        self.tableView.data = muArray;
        self.tableView.eventDelegate = self;
        [self.tableView reloadData];
        
        //如果需要实现上拉加载，则只需要
        //self.tableView.isMoreInfo = true;
        //当我们判断服务器还有数据可以加载时便可开启此开关
        
        
        //调用父类写好的网络请求成功方法requestSuccess
        //如果需要自定义请求成功后的处理则可在此类中覆盖requestSuccess,暂时不模拟网络失败的情况
        [self requestSuccess];
        
    });
}

#pragma mark 重新请求网络
- (void)requestDataAgain
{
    [super requestDataAgain];
    [self requestData];
}



#pragma mark--------------------------------UITableViewEventDelegate
#pragma mark 下拉刷新
- (void)pullDown:(BaseTableView *)tableView
{
    [super pullDown:self.tableView];
    [self requestData];
}

//#pragma mark 上拉加载 根据情况是否需要有上拉加载
//- (void)pullUp:(BaseTableView *)tableView
//{
//
//    [super pullUp:self.tableView];
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
